from midi import prompt_for_channel, Channel

pitches = 'C Db D Eb E F Gb G Ab A Bb B'.split()

channel = Channel(prompt_for_channel())

class Note(object):
    def __init__(self, pitch, octave):
        self.name = '%s%s' % (pitch, octave)
        self.midi_id = (octave + 2)*12 + pitches.index(pitch) - 1
        self.octave = octave
        self.pitch = pitch

    def play(self):
        channel.startnote(self.midi_id)
    
    def stop(self):
        channel.stopnote(self.midi_id)
       
    def __str__(self):
        return 'Note: %s midi: %s' % (self.name, self.midi_id)
        
    __repr__ = __str__

notes = []
for octave in range(1,6):
    for pitch in pitches:
        notes.append(Note(pitch, octave))

_d = dict((n.name, n) for n in notes)
def getnotebyname(name):
    return _d[name]
    
def noterange(start, end):
    '''
    e.g: noterange('C3' ,'C4') is all the notes between C3 and C4 inclusive
    '''
    midistart = getnotebyname(start).midi_id
    midiend = getnotebyname(end).midi_id
    return [n for n in notes if (midistart < n.midi_id <= midiend)]
